package org.nokatag.tagjava;

import java.io.IOException;
import java.util.HashSet;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.nokatag.system.Concest;
import org.nokatag.system.Function;

public class ImageUpload extends NokaTag{

	private String  name=null;
	private String  id=null;
	private String  width="160";
	private String  height="120";
	private String  msg=null;
	private String  filesize="500";//上传文件大小
	private String  uid = "99999999";//用户自定义过滤id
	private String  isdel = "yes";//是否可以删除
	private String  type="pid";//过滤类型，uid,sid,pid
	private String  value=null;
	private String  onchange=null;//在值改变时调用的方法
	private String  uploadfunction = null;//上传外部调用方法
	private String title=null;
	private String readonly=null;//是否只读
	private String disabled = null;
	private Integer textsun=50;//备注字数
	private static final Integer textsunMax=100;
	private String utype="1";//1,为上传，2为拍照,3为上传加拍照
	private static final String NOKA_FILE_UOLOAD_JAVASCRIPT_FUNCTION = "NOKA_FILE_UOLOAD_JAVASCRIPT_FUNCTION";
	/**
	 * 
	 */
	private static final long serialVersionUID = 4048637154410931426L;
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		String basePath = basepath(request);
		JspWriter jspOut = pageContext.getOut();
		StringBuilder bodystr = new StringBuilder();
		UUID uuid = UUID.randomUUID();
		//-------------------设置文件类型------------------
		if(Concest.UPDOCTYPE==null){
			Concest.UPDOCTYPE=new HashSet<String>();
		}
		Concest.UPDOCTYPE.add("jpg");
		Concest.UPDOCTYPE.add("gif");
		Concest.UPDOCTYPE.add("png");
		//-------------------------------------------------
		String sessionid = uid+","+pageContext.getSession().getId()+","+uuid.toString().replace("-", "_");
		bodystr.append(potjs(request));//加载Prototyp
		bodystr.append(potimageuploadjs(request));//加载upfile.js
		bodystr.append(nkBoxJsAndCss(request));
		FormHtmlInsertid(getId(),getName(),getValue());
		bodystr.append("<div id=\""+getId()+"_div\"></div>");//加载Prototype
		bodystr.append("<script type=\"text/javascript\">");
		bodystr.append("Event.observe(document, 'dom:loaded', function() {");
		bodystr.append("var upfs"+getId()+" = {");
		bodystr.append("id:'"+getId()+"',");
		bodystr.append("name:'"+getName()+"',");
		bodystr.append("value:'"+(value==null?"":value)+"',");
		bodystr.append("filesize:'"+(filesize==null?"512":filesize)+"',");
		bodystr.append("uid:'"+(uid==null?"99999999":uid)+"',");
		bodystr.append("isdel:'"+("false".equalsIgnoreCase(isdel)?"no":"yes")+"',");
		bodystr.append("type:'"+(type==null?"pid":type)+"',");
		bodystr.append("idreadio:true,");
		bodystr.append("readonly:"+("readonly".equals(readonly))+",");
		bodystr.append("disabled:"+("disabled".equals(disabled))+",");
		bodystr.append("textsun:'"+((textsun==null || textsun>textsunMax)?textsunMax:textsun)+"',");
		bodystr.append("title:'"+(title==null?"":title)+"',");
		bodystr.append("rurl:'"+basePath+"',");
		bodystr.append("width:'"+width+"',");
		bodystr.append("height:'"+height+"',");
		bodystr.append("utype:'"+utype+"',");
		bodystr.append("nokatag_image_poet:'"+Function.lung(request, "nokatag_image_poet")+"',");
		//--------------------------------------------------------------------------------------------------
		if(getMsg()==null){
			if("2".equals(utype))
				bodystr.append("nokatag_image_poeter:'"+Function.lung(request, "nokatag_image_poeter")+"',");
			else
				bodystr.append("nokatag_image_poeter:'"+Function.lung(request, "nokatag_image_msg")+"',");
		}
		else
			bodystr.append("nokatag_image_poeter:'"+getMsg()+"',");
		//---------------------------------------------------------------------------------------
		if(getMsg()==null)
			bodystr.append("nokatag_image_msg:'"+Function.lung(request,"nokatag_image_msg")+"',");
		else
			bodystr.append("nokatag_image_msg:'"+getMsg()+"',");
		bodystr.append("sessionid:'"+sessionid+"',");
		bodystr.append("onchange:function(fname,fid,frurl,fdurl){"+(onchange==null?"":""+onchange+"(fname,fid,frurl,fdurl);")+"}");
		bodystr.append("};");
		bodystr.append(" var upfsfieupload"+getId()+" = new nimageUpload(upfs"+getId()+");");
		bodystr.append(" upfsfieupload"+getId()+".show();");
		bodystr.append("});");
//		bodystr.append(" function noka_tag_upload_file_redata_"+getId()+"(fname,fid,frurl,fdurl){$('"+getId()+"').uploadRedata(fname,fid,frurl,fdurl);}");
//		bodystr.append(" function noka_tag_upload_file_setvalue_"+getId()+"(fid){$('"+getId()+"').setValue(fid);}");
//		bodystr.append(" function noka_tag_upload_file_pclose_"+getId()+"(){try{NokaBox.box.hide();}catch(e){}}");//关闭
//		
		if(request.getAttribute(NOKA_FILE_UOLOAD_JAVASCRIPT_FUNCTION)==null || request.getAttribute(NOKA_FILE_UOLOAD_JAVASCRIPT_FUNCTION).equals(false)){
			request.setAttribute(NOKA_FILE_UOLOAD_JAVASCRIPT_FUNCTION,true);
			bodystr.append(" function noka_tag_upload_file_redata(id,fname,fid,frurl,fdurl){$(id).uploadRedata(fname,fid,frurl,fdurl);}");
			bodystr.append(" function noka_tag_upload_file_setvalue(id,fid){$(id).setValue(fid);}");
			bodystr.append(" function noka_tag_upload_file_pclose(){try{NokaBox.box.hide();}catch(e){}}");//关闭
		}
		if(uploadfunction!=null){
			bodystr.append(" function "+uploadfunction+"(){$('"+getId()+"').uploadFile()}");
		}
	    //--------------------------------------------------------
		if("2".equals(utype) || "3".equals(utype)){
			//--------------成功------------------------
			bodystr.append("function "+id+"_resufunction(v){");
			bodystr.append("$('"+getId()+"').setValue(v);");
			bodystr.append("}");
			//--------------失败------------------------
			bodystr.append("function "+id+"_reofunction(v){");
			bodystr.append("$(\"image_"+getId()+"\").innerHTML=\""+Function.lung(request, "nokatag_image_uperro")+"\"; ");
			bodystr.append("}");
		}
		bodystr.append("</script>"); 
		try {
			jspOut.write(bodystr.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_BODY_INCLUDE;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getWidth() {
		return width;
	}
	public void setWidth(String width) {
		this.width = width;
	}
	public String getHeight() {
		return height;
	}
	public void setHeight(String height) {
		this.height = height;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getFilesize() {
		return filesize;
	}
	public void setFilesize(String filesize) {
		this.filesize = filesize;
	}
	public String getUid() {
		return uid;
	}
	public void setUid(String uid) {
		this.uid = uid;
	}
	public String getIsdel() {
		return isdel;
	}
	public void setIsdel(String isdel) {
		this.isdel = isdel;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getOnchange() {
		return onchange;
	}
	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}
	public String getUploadfunction() {
		return uploadfunction;
	}
	public void setUploadfunction(String uploadfunction) {
		this.uploadfunction = uploadfunction;
	}
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public Integer getTextsun() {
		return textsun;
	}
	public void setTextsun(Integer textsun) {
		this.textsun = textsun;
	}
	public String getUtype() {
		return utype;
	}
	public void setUtype(String utype) {
		this.utype = utype;
	}
	public String getReadonly() {
		return readonly;
	}
	public void setReadonly(String readonly) {
		this.readonly = readonly;
	}
	public String getDisabled() {
		return disabled;
	}
	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

}
